package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis.aibkAnalysisZW

import aibkTestReview.aiPhotoPick.photoViewStyle
import aibkTestReview.aiPhotoPick.photoViewStyle.mPictureParameterStyle
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis.AnalysisInforIndexHelp.getChildListBean
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aiPhotoPick.FullyGridLayoutManager
import com.lancoo.znbkxx.teacher.aibkTestCoach.aiPhotoPick.GlideEngine
import com.lancoo.znbkxx.teacher.aibkTestCoach.aiPhotoPick.photoAnalysisGridImageAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseFragment
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperPerAnalysisInfor
import com.lancoo.znbkxx.teacher.utils.WebStyleUtils
import com.lancoo.znbkxx.teacher.widget.CustomTextViewToPicture
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.decoration.GridSpacingItemDecoration
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.tools.ScreenUtils
import kotlinx.android.synthetic.main.aibk_adapter_item_itemanalysis.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_itemanswer_zw.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_itemscore.view.*
import kotlinx.android.synthetic.main.aibk_fragment_child_zw_analysis_image.view.*
import kotlinx.android.synthetic.main.aibk_fragment_child_zw_analysis_word.view.TvQuesNumber

class AnalysisChildFargmentImage(
        val activity:AppCompatActivity,
        val paperPerAnalysisInfor: PaperPerAnalysisInfor,
        val index_A:Int,
        val index_B:Int,
        val index_C:Int
) : KBaseFragment() {

    private var rootView:View? = null
    private var selectList = mutableListOf<LocalMedia>()


    override fun createLayout(): Int {
        return R.layout.aibk_fragment_child_zw_analysis_image
    }

    override fun initView(view: View?) {
        rootView = view

        //显示序号
        val itemSortIndex = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ItemList[0].ItemSortIndex
        rootView!!.TvQuesNumber.text = "$itemSortIndex. "

        //显示作答得分
        val evalScore = paperPerAnalysisInfor.TypeList[index_A].QuesAnswerList[index_B].ChildAnswerList[0].ItemList[0].EvalScore
        val scoreRate = paperPerAnalysisInfor.TypeList[index_A].QuesAnswerList[index_B].ChildAnswerList[0].ScoreRate
        val state = paperPerAnalysisInfor.TypeList[index_A].QuesAnswerList[index_B].ChildAnswerList[0].State
        if(state == 0){
            rootView!!.TvItemScoreTitle.setTextColor(activity.resources.getColor(R.color.lancoooBlue));
            rootView!!.TvItemScore.setTextColor(activity.resources.getColor(R.color.lancoooBlue));
            rootView!!.TvItemScore.text = "--"
        }else{
            if(scoreRate>=0.6){
                rootView!!.TvItemScoreTitle.setTextColor(activity.resources.getColor(R.color.lancoooGreen));
                rootView!!.TvItemScore.setTextColor(activity.resources.getColor(R.color.lancoooGreen));
            }else{
                rootView!!.TvItemScoreTitle.setTextColor(activity.resources.getColor(R.color.lancoooRed));
                rootView!!.TvItemScore.setTextColor(activity.resources.getColor(R.color.lancoooRed));
            }
            rootView!!.TvItemScore.text = evalScore+"分"
        }

        //显示学生照片作答
        val imagePath = paperPerAnalysisInfor.TypeList[index_A].QuesAnswerList[index_B].ChildAnswerList[0].ItemList[0].ImagePath
        if(imagePath == null||imagePath.isEmpty()) {
            rootView!!.RvPhotoAnswer.visibility = View.GONE
        }else{
            rootView!!.RvPhotoAnswer.visibility = View.VISIBLE
            photoViewStyle.getWeChatStyleAnalysis(activity)
            val manager = FullyGridLayoutManager(
                    activity, 3, GridLayoutManager.VERTICAL, false
            )
            rootView!!.RvPhotoAnswer.layoutManager = manager
            rootView!!.RvPhotoAnswer.addItemDecoration(
                    GridSpacingItemDecoration(3, ScreenUtils.dip2px(activity, 8f), false)
            )


            if(imagePath.isNotEmpty()) {
                var imagePathSize = LancooMethod.getKeyCount(imagePath, "|") + 1
                for(i in 0 until imagePathSize) {
                    var SingleimagePath = LancooMethod.returnArray(imagePath,"|",1)!![i]
                    val media = LocalMedia()
                    val obshttp = Ext.OBSINFO?.ServerWithBucket
                    media.path = obshttp + SingleimagePath
                    media.compressPath = obshttp + SingleimagePath
                    selectList.add(i,media)
                }
            }

            val mPhotoAdapter = photoAnalysisGridImageAdapter(activity)
            mPhotoAdapter!!.setList(selectList)
            mPhotoAdapter!!.setSelectMax(9)
            rootView!!.RvPhotoAnswer.adapter = mPhotoAdapter
            mPhotoAdapter!!.setOnItemClickListener { v, position ->
                val selectList: List<LocalMedia> = mPhotoAdapter!!.data
                if (selectList.isNotEmpty()) {
                    val media = selectList[position]
                    val mimeType = media.mimeType
                    val mediaType = PictureMimeType.getMimeType(mimeType)
                    when (mediaType) {
                        PictureConfig.TYPE_IMAGE ->                         // 预览图片 可自定长按保存路径
                            PictureSelector.create(activity)
                                    .setPictureStyle(mPictureParameterStyle)// 动态自定义相册主题
                                    .isNotPreviewDownload(true)// 预览图片长按是否可以下载
                                    .imageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                                    .openExternalPreview(position, selectList)

                    }
                }
            }
        }

        if(imagePath==null||imagePath.isEmpty()){
            rootView!!.LlNoPhotoAnswer.visibility = View.VISIBLE
        }else{
            rootView!!.LlNoPhotoAnswer.visibility = View.GONE
        }
        
        //显示参考范文
        var childAnswer = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ChildAnswer
        WebStyleUtils.showWebViewCssStyle(activity, rootView!!.WvItemAnswer, childAnswer, false)

        //显示答案解析
        var childAnalysis = getChildListBean(paperPerAnalysisInfor, index_A, index_B, index_C).ChildAnalysis
        if (childAnalysis.isEmpty()) {
            childAnalysis = activity.getString(R.string.aibk_no_text) //...略
        }
        CustomTextViewToPicture(activity, rootView!!.TvItemAnalysis, childAnalysis)
    }

}